package cn.wolfcode.luowowo.search.service.impl;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.StrategyTheme;
import cn.wolfcode.luowowo.article.query.DestFilterQuery;
import cn.wolfcode.luowowo.article.service.IStrategyThemeService;
import cn.wolfcode.luowowo.scenic.domain.Scenic;
import cn.wolfcode.luowowo.scenic.service.IScenicService;
import cn.wolfcode.luowowo.scenic.vo.DestFilterVO;
import cn.wolfcode.luowowo.search.domain.DestinationTemplate;
import cn.wolfcode.luowowo.search.service.IDestinationTemplateSearchService;
import cn.wolfcode.luowowo.search.service.repository.DestinationTemplateRepository;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import org.apache.commons.beanutils.BeanUtils;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

@Service
public class DestinationTemplateSearchServiceImpl implements IDestinationTemplateSearchService {
    @Autowired
    private DestinationTemplateRepository repository;
    @Autowired
    private ElasticsearchTemplate template;

    @Reference
    private IStrategyThemeService strategyThemeService;
    @Reference
    private IScenicService scenicService;
    //用户的更新或者保存操作
    public void saveOrUpdate(DestinationTemplate destinationTemplate) {
        repository.save(destinationTemplate);//如果没有这个对象就是保存操作,有就是更新操作
    }

    public DestinationTemplate findByName(String name) {
        return repository.findByName(name);
    }

    public List<DestFilterVO> queryDestFilter(DestFilterQuery qo) {
        TermQueryBuilder month = null;
        RangeQueryBuilder days = null;
        MatchQueryBuilder theme = null;
        NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
        if(qo.getTimeId()!=-1){
            String num = null;
            if(qo.getTimeId()==1){
                num = "一月";
            }
            if(qo.getTimeId()==2){
                num = "二月";
            }
            if(qo.getTimeId()==3){
                num = "三月";
            }
            if(qo.getTimeId()==4){
                num = "四月";
            }
            if(qo.getTimeId()==5){
                num = "五月";
            }
            if(qo.getTimeId()==6){
                num = "六月";
            }
            if(qo.getTimeId()==7){
                num = "七月";
            }
            if(qo.getTimeId()==8){
                num = "八月";
            }
            if(qo.getTimeId()==9){
                num = "九月";
            }
            if(qo.getTimeId()==10){
                num = "十月";
            }
            if(qo.getTimeId()==11){
                num = "勾";
            }
            if(qo.getTimeId()==12){
                num = "蛋";
            }
            month = QueryBuilders.termQuery("month", num);
        }
        if(qo.getThemeId()!=-1){
            StrategyTheme st = strategyThemeService.selectById(qo.getThemeId());
             theme = QueryBuilders.matchQuery("theme", st.getName());
        }
        if(qo.getDayId()!=null){
             days = QueryBuilders.rangeQuery("days").gte(qo.getDayId().getMin()).lte(qo.getDayId().getMax());
        }

        if(month!=null){
            builder.withQuery(QueryBuilders.boolQuery().must(month));
        }
        if(days !=null){
            builder.withQuery(QueryBuilders.boolQuery().must(days));
        }
        if(theme!=null){
            builder.withQuery(QueryBuilders.boolQuery().must(theme));
        }
        if(month!=null&&days!=null){
            builder.withQuery(QueryBuilders.boolQuery().must(month).must(days));
        }
        if(month!=null&&theme!=null){
            builder.withQuery(QueryBuilders.boolQuery().must(month).must(theme));
        }
        if(days!=null&&theme!=null){
            builder.withQuery(QueryBuilders.boolQuery().must(theme).must(days));
        }
        if(days!=null&&theme!=null&&month!=null){
            builder.withQuery(QueryBuilders.boolQuery().must(month).must(theme).must(days));
        }
        builder.withPageable(PageRequest.of(0,100));
        Page<DestinationTemplate> search = repository.search(builder.build());
        List<DestinationTemplate> dts = search.getContent();
        List<DestFilterVO> vos = new ArrayList<>();
        for (DestinationTemplate dt:dts){
            DestFilterVO vo = new DestFilterVO();
            Destination destination = new Destination();
            destination.setId(dt.getId());
            try {
                BeanUtils.copyProperties(destination,dt);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            vo.setDset(destination);
            List<Scenic> top3Scenic = scenicService.getTop3Scenic(dt.getId());
            vo.setScenics(top3Scenic);
            vos.add(vo);
        }
        return vos;
    }
}
